package cn.lopr.im.android.mvp.presenter;

import java.util.List;

import cn.lopr.im.android.bean.Article;
import cn.lopr.im.android.bean.ArticlePlate;
import cn.lopr.im.android.bean.ArticleVo;
import cn.lopr.im.android.bean.CallbackResult;
import cn.lopr.im.android.bean.User;
import cn.lopr.im.android.common.OnCallBackListener;
import cn.lopr.im.android.mvp.contact.IArticlePublishContact;
import cn.lopr.im.android.mvp.contact.ICommunityContact;
import cn.lopr.im.android.mvp.model.CommunityModel;
import cn.lopr.im.android.utils.UiUtils;

/**
 * @author shuangyang7
 * @date 2021/5/23 13:24
 */
public class ArticlePublishPresenterImpl implements IArticlePublishContact.IArticlePublishPresenter {
    private String TAG;
    private final IArticlePublishContact.IArticlePublishView articlePublishView;
    private final ICommunityContact.ICommunityModel communityModel;

    public ArticlePublishPresenterImpl(IArticlePublishContact.IArticlePublishView articlePublishView) {
        this.articlePublishView = articlePublishView;
        this.communityModel = CommunityModel.getInstance();
    }

    @Override
    public void getArticle(String token) {
        communityModel.getArticle(token, this.articlePublishView.getArticleId(), new OnCallBackListener<CallbackResult<ArticleVo>, String>() {
            @Override
            public void success(CallbackResult<ArticleVo> data) {
                articlePublishView.getArticleSuccess(data.getData());
            }

            @Override
            public void failure(String errMsg) {
                articlePublishView.showVerifyFailed(errMsg);
            }
        });
    }

    @Override
    public void addArticle(String token, User user) {
        if (!UiUtils.isNetworkAvailable()) {
            articlePublishView.showNetworkError();
            return;
        }

        Article article = new Article().setContent(this.articlePublishView.getContent())
                .setPublishUserId(user.getId()).setPlateId(this.articlePublishView.getPlateId());
        communityModel.addArticle(token, article, new OnCallBackListener<CallbackResult<String>, String>() {
            @Override
            public void success(CallbackResult<String> data) {
                articlePublishView.addArticleSuccess();
                articlePublishView.disLoading();
                articlePublishView.showVerifyFailed(data.getMsg());
            }

            @Override
            public void failure(String errMsg) {
                articlePublishView.showVerifyFailed(errMsg);
                articlePublishView.disLoading();
            }
        });
    }

    @Override
    public void updArticle(String token) {
        Article article = new Article().setId(this.articlePublishView.getArticleId())
                .setContent(this.articlePublishView.getContent())
                .setPlateId(this.articlePublishView.getPlateId());

        this.communityModel.updateArticle(token, article, new OnCallBackListener<CallbackResult<String>, String>() {
            @Override
            public void success(CallbackResult<String> data) {
                articlePublishView.updArticleSuccess();
                articlePublishView.disLoading();
                articlePublishView.showVerifyFailed(data.getMsg());
            }

            @Override
            public void failure(String errMsg) {
            }
        });
    }

    @Override
    public void getPlate(String token) {
        this.communityModel.getPlates(token, new OnCallBackListener<CallbackResult<List<ArticlePlate>>, String>() {
            @Override
            public void success(CallbackResult<List<ArticlePlate>> data) {
                articlePublishView.getPlatesSuccess(data.getData());
            }

            @Override
            public void failure(String errMsg) {

            }
        });
    }
}
